<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" v-auth="'system:role:update'" @click="handleCreate">
          新增角色
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              tooltip: '编辑角色资料',
              auth: 'system:role:update',
              onClick: handleUpdate.bind(null, record),
            },
            {
              icon: 'ant-design:solution-outlined',
              tooltip: '用户角色绑定',
              auth: 'system:role:update',
              onClick: handleUserRole.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              auth: 'system:role:delete',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
    <UserRoleDrawer @register="registerDrawer1" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { pageList, deleteRole } from './role.api';

  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from './RoleDrawer.vue';
  import UserRoleDrawer from './UserRoleDrawer.vue';

  import { columns, searchFormSchema } from './role.data';

  export default defineComponent({
    name: 'RoleManagement',
    components: { BasicTable, RoleDrawer, UserRoleDrawer, TableAction },
    setup() {
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerDrawer1, { openDrawer: openUserRoleDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: '角色列表',
        api: pageList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleCreate() {
        openDrawer(true, { isUpdate: false });
      }

      function handleUpdate(record: Recordable) {
        openDrawer(true, { record, isUpdate: true });
      }

      function handleUserRole(record: Recordable) {
        openUserRoleDrawer(true, { roleId: record.id, isUpdate: true });
      }

      function handleDelete(record: Recordable) {
        deleteRole([record.id], reload);
      }

      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        registerDrawer,
        registerDrawer1,
        handleCreate,
        handleUpdate,
        handleUserRole,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
